package com.third.clock.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.third.clock.R;
import com.third.clock.bean.LoginEntity;
import com.third.clock.bean.ResponseData;
import com.third.clock.manager.ClockManager;
import com.third.clock.ui.ValidateCodeActivity;

/**
 * Created by Administrator on 2018/7/23 0023.
 */

public class MeFragment extends Fragment implements IBaseFragment,View.OnClickListener{

    protected Activity ctx;

    protected View layout;

    private TextView mTextViewName;

    private TextView mTextViewSex;

    private TextView mTextViewCardno;

    private View mLayoutEditPassword;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (layout == null) {
            ctx = this.getActivity();
            layout = ctx.getLayoutInflater().inflate(R.layout.fragment_me, null);

        } else {
            ViewGroup parent = (ViewGroup) layout.getParent();
            if (parent != null) {
                parent.removeView(layout);
            }
        }
        return layout;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        paserData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_editpassword:
                 startActivity(new Intent(getActivity(), ValidateCodeActivity.class));
                break;
        }
    }

    @Override
    public void initEvent() {
        paserData();
    }

    private void initView(){
        mTextViewName= (TextView) layout.findViewById(R.id.textview_name);
        mTextViewSex= (TextView) layout.findViewById(R.id.textview_sex);
        mTextViewCardno=(TextView) layout.findViewById(R.id.textview_cardno);
        mLayoutEditPassword=layout.findViewById(R.id.layout_editpassword);

        mLayoutEditPassword.setOnClickListener(this);
    }

    private void paserData(){
        ClockManager clockManager=ClockManager.getInstace(getActivity());
        if(clockManager!=null){
            ResponseData<LoginEntity> responseData=clockManager.mLoginResponseData;
            if(responseData!=null&&responseData.getData()!=null){
                String name=responseData.getData().getName();
                if(!TextUtils.isEmpty(name)){
                    if(mTextViewName!=null)mTextViewName.setText(name);
                }
                String sex=responseData.getData().getSex();
                if(!TextUtils.isEmpty(sex)){
                    if(sex.equals("1")){
                        if(mTextViewSex!=null) mTextViewSex.setText("男");
                    }else{
                        if(mTextViewSex!=null) mTextViewSex.setText("女");
                    }
                }
                String cardno=responseData.getData().getCardno();
                if(!TextUtils.isEmpty(cardno)){
                   if(mTextViewCardno!=null) mTextViewCardno.setText(cardno);
                }
            }
        }
    }

}
